package messages;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import org.apache.bcel.generic.GOTO;

import ocsf.server.ConnectionToClient;
import controllers.DBConnection;
import controllers.WorkerController;
import controllers.CustomerController;
import entity.Picture;
import entity.User;
import entity.UserLoginInfo;
import controllers.EchoServer;
import GUI.CustomerGui;
import GUI.MapScale;


/**
 * this class is handling with the search picture message by making a connection with DB
 */
public class SearchPicMessage {

	String[] picturearr;
	ConnectionToClient client;
	WorkerController workerC;
	CustomerController customerC;
	UserLoginInfo loginInfo;
	//EchoServer echo;
	

	Picture picture;
	private String Place ; 
	private String Time;
	private String PicID;
	private String finaly;
	private String picIdentety;
	public String url;
	public String url1;
	public String price;
	int picIDD;
	static String customerID;
	String ss;
	double bill;
	
	public SearchPicMessage() {}
	
	public SearchPicMessage(String[] msg, ConnectionToClient client)
	{
		this.picturearr=msg;
		this.client=client;	
		
	}
	public void activationSearch() throws IOException
	{

			int len;
			byte[] fileBytes;
		
			customerID=picturearr[3];
			try{
					/**
					 *  sql connection
					 */
					 
					DBConnection conn=new DBConnection();
					java.sql.PreparedStatement pr1= conn.getDBConnection().prepareStatement("SELECT picID ,picPrice " + "FROM sgi_system.archive " + "WHERE  picPlace =? AND picTime=?");
					pr1.setString(1,picturearr[1]);
					pr1.setString(2,picturearr[2]);
					ResultSet ppp=pr1.executeQuery();

					
					/**
					 * if no such pic in DB
					 */
					if (!ppp.next() ) {
					    System.out.println("No image in the DB");
					    JOptionPane.showMessageDialog(null, "No image in the DB ");
					    return;
					 
					} else {
						/**
						 * url of the specific picture handle
						 */
					    do {
					    	price=ppp.getString("picPrice");
					    	 picIDD=ppp.getInt(1);
							 PicID=Integer.toString(picIDD);
							 String s1=".jpg";
							 String s2="images/";
							  finaly=s2+PicID+s1;
							  picIdentety= PicID+s1;
							System.out.println("->>>>Wanted Picture Id:\t"+PicID);
					    } while (ppp.next());
					}
					
					
						
					
						
				
							
			}//try
				catch (Exception e){e.printStackTrace();}
	 
	 
	
			try
				{
						/**
						 * get image
						 */
					DBConnection conn1=new DBConnection();
					java.sql.PreparedStatement pr8= conn1.getDBConnection().prepareStatement("SELECT * FROM sgi_system.image WHERE picID=? ");
					pr8.setString(1,picIdentety);
					ResultSet rs = pr8.executeQuery();
					
					
					
					int selectedOption=JOptionPane.showConfirmDialog(null, "Do you want to buy this picture", "Buy picture", JOptionPane.YES_NO_OPTION);/*confirm buying picture*/
					if (selectedOption == JOptionPane.YES_OPTION) {
						
						
						/*check if he bought the picture already*/
						java.sql.PreparedStatement pr1 = conn1.getDBConnection().prepareStatement("SELECT picID FROM sgi_system.purchasespic WHERE customerID=?;");
						pr1.setString(1, customerID);
						ResultSet rs1=pr1.executeQuery();
						
						while(rs1.next()){
							
							ss=rs1.getString("picID");
							if(Integer.valueOf(ss)==picIDD){
								
								JOptionPane.showMessageDialog(null, "You already bought this picture");
								return;
							}
						}
							/*update customer bill*/
							
							java.sql.PreparedStatement pr10 = conn1.getDBConnection().prepareStatement("SELECT bill FROM sgi_system.customer WHERE customerID=?;");
							pr10.setString(1, customerID);
							ResultSet rs10=pr10.executeQuery();
							while(rs10.next()){
								bill=rs10.getDouble("bill");
								bill=bill+Double.valueOf(price);
								System.out.println(bill);
							}
							
							
							java.sql.PreparedStatement pr11 = conn1.getDBConnection().prepareStatement("UPDATE   sgi_system.customer  SET bill=?    WHERE customerID=?;");
							pr11.setString(2, customerID);
							pr11.setDouble(1, bill);
							pr11.executeUpdate();
							
							
						
					   
						java.sql.PreparedStatement pr2 = conn1.getDBConnection().prepareStatement("INSERT INTO sgi_system.purchasespic (customerID,picID) VALUES(?,?);");
						pr2.setInt(1,Integer.valueOf(picturearr[3]) );
						pr2.setInt(2, picIDD);			
						pr2.executeUpdate();
					while(rs.next())
						{
			                fileBytes = rs.getBytes(3);
			                url=("C:/picc//image//");
			                url1=picIdentety;
			                OutputStream targetFile=new FileOutputStream(url+url1);
			                targetFile.write(fileBytes);
			                targetFile.close();
						}//while
					}
				
					else return;
				}//try
				catch (Exception e){e.printStackTrace();}

				/**
				 * open requested picture with zoom MapScale class
				 */
		
				 MapScale test = new MapScale(url+url1);
				 test.Run();
	
	  
}
	

	 public String getUrl() 
	 {
		return (this.url+this.url1);
	 }
	 public void setUrl(String s1) 
	 {
	
		this.url1=s1;
	 }
	 public String getCustID() 
	 {
		return (customerID);
	 }
}
